package top.lyc829.iot_android;

import androidx.annotation.NonNull;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.DisplayMetrics;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.xuexiang.xpage.core.PageOption;
import com.xuexiang.xpage.enums.CoreAnim;

import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.util.List;

import kotlin.jvm.functions.Function1;
import top.lyc829.iot_android.core.BaseActivity;
import top.lyc829.iot_android.mqtt.MqttManager;
import top.lyc829.iot_android.util.Utils;

public class MainActivity extends BaseActivity {

    MqttManager mqttManager = MqttManager.getInstance();

    private static void setCustomDensity(@NonNull Activity activity, @NonNull Application application) {
        final DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();
        final float targetDensity = appDisplayMetrics.widthPixels / 360;
        final int targetDensityDpi = (int) (160 * targetDensity);

        appDisplayMetrics.density = appDisplayMetrics.scaledDensity = targetDensity;
        appDisplayMetrics.densityDpi = targetDensityDpi;

        final DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
        activityDisplayMetrics.density = appDisplayMetrics.scaledDensity = targetDensity;
        activityDisplayMetrics.densityDpi = targetDensityDpi;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        XXPermissions.with(this)
                .permission(Permission.READ_PHONE_STATE)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            goPage();

                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            Utils.showToast("被永久拒绝授权，请手动授予获取手机信息权限");
                            finish();
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            //XXPermissions.startPermissionActivity(MainActivity.this, permissions);
                        } else {
                            Utils.showToast("获取手机信息权限失败");
                            finish();
                        }
                    }
                });

        mqtt();

    }

    private void mqtt() {

        mqttManager.init();
        mqttManager.connect();
        mqttManager.addSubscribe("/lyc", mqttMessage -> {
            Utils.SoutMqttd(mqttMessage.toString());
        });

    }

    private void goPage() {
        Utils.Sout("获取手机信息权限成功");
        PageOption.to("MainFragment") //跳转的fragment
                .setAnim(CoreAnim.zoom) //页面跳转动画
                .setRequestCode(100) //请求码，用于返回结果
                .setAddToBackStack(true) //是否加入堆栈
                .setNewActivity(false) //是否使用新的Activity打开
                .open(MainActivity.this); //打开页面进行跳转

    }

    @Override
    protected void onDestroy() {
        mqttManager.destroy();
        super.onDestroy();

    }
}